#ifndef GPU_H
#define GPU_H

#include <QObject>
#include "gpu_global.h"
#include "mymath.h"
#include <QPixmap>

#define GpuInstance GPU::getInstance()

enum TEXTURE_WRAP{TW_REPEAT, TW_MIRROR};

#define FRACTION(v)	((v) - (int)(v))


class GPU_EXPORT GPU : public QObject
{
    Q_OBJECT
public:
    static GPU* getInstance() {return &m_instance;}

    QPixmap getPixmap();
    void clearPixmap();

    void initGpu();

    void drawPoint(const Point& p);

    void drawLine(const Point& p1, const Point& p2);

    void drawTriangle(const Point& p1, const Point& p2, const Point& p3);

    void drawUVTriangle(const Point& p1, const Point& p2, const Point& p3,const char* imageName);

    void drawImage(const char* imageName);

    void setBilinearEnable(bool enable);

    void setTextureWrap(TEXTURE_WRAP texture);

private:
    explicit GPU(QObject *parent = nullptr);
    ~GPU();

    static GPU m_instance;

    QPixmap *mp_pixmap;
    int m_width;
    int m_height;

    //图片像素采样
    bool m_bilinearEnable = false;
    Color sampleNearest(QImage& image, const math::vec2f& uv);
    Color sampleBilinear(QImage& image, const math::vec2f& uv);

    //采样方式
    TEXTURE_WRAP m_wrap = TW_REPEAT;
    void checkWrap(float& n);
};



#endif // GPU_H
